package com.xywy.me;

import java.util.HashMap;

import org.json.JSONObject;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.net.AsyncHttpClient;
import com.xywy.common.net.OnResultListenerAdapter;
import com.xywy.common.net.Parser;
import com.xywy.common.util.MLog;
import com.xywy.common.widget.MyProgress;
import com.xywy.common.widget.TitleBar2;
import com.xywy.me.bean.UserKeeper;

public class FeedbackActivity extends BaseActivity implements OnClickListener {
	private EditText mContent; // 内容
	private Button mSubmit; // 提交
	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			MLog.i(TAG, "back:" + msg.obj + "resuletCode:" + msg.arg1);
			myProgress.stop();
			int resultCode = msg.arg1;

			if (resultCode == 200) {
				Toast.makeText(FeedbackActivity.this, "反馈成功 谢谢", 1).show();
				finish();
			} else /**if (resultCode == 500)*/ {
				try {
			        if(msg.obj != null){
			        	JSONObject json = new JSONObject(msg.obj.toString());
			        	int state = json.optInt("state");
			        	if(state == 500){
			        		Toast.makeText(FeedbackActivity.this,json.optString("error"), 1).show();
			        	}else {
							Toast.makeText(FeedbackActivity.this, "反馈失败", 1).show();
						}
			        }else {
			        	Toast.makeText(FeedbackActivity.this, "反馈失败", 1).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	};
	private MyProgress myProgress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置为全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.me_feedback);
		this.preInit();
		this.initView();
		this.loadData();
	}

	@Override
	public void preInit() {
		// TODO Auto-generated method stub

	}

	@Override
	public void loadData() {
		// TODO Auto-generated method stub

	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		TitleBar2 titleBar = (TitleBar2) findViewById(R.id.me_feed_title_bar);
		titleBar.onClickBack(this);
		TextView tvTitle = titleBar.getTitle();
		TextView tvBack = titleBar.getTvBack();
		titleBar.getGo().setVisibility(View.INVISIBLE);
		tvTitle.setText(getString(R.string.me_feedback_title));
		tvBack.setText(getString(R.string.me_feedback_back));
		mContent = (EditText) findViewById(R.id.content_feedback);
		mContent.setOnFocusChangeListener(mOnFocusChangeListener);
		mSubmit = (Button) findViewById(R.id.btn_feedback);
		mSubmit.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_feedback:
			String content = mContent.getText().toString().trim();
			if (content == null || "".equals(content)) {
				Toast.makeText(this, "请输入您的意见或建议", 1).show();
				return;
			} else {
				// act=kbb&fun=user_feedback
				feedBack(content);
			}
			break;

		default:
			break;
		}
	}

	private OnFocusChangeListener mOnFocusChangeListener = new OnFocusChangeListener() {
		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			EditText textView = (EditText) v;
			String hint;
			if (hasFocus) {
				hint = textView.getHint().toString();
				textView.setTag(hint);
				textView.setHint("");
			} else {
				hint = textView.getTag().toString();
				textView.setHint(hint);
			}
		}
	};

	private void feedBack(String content) {
		myProgress = new MyProgress(this, 10);
		myProgress.start();
		Parser paramsParser = new Parser();

		// 封装参数
		HashMap params = new HashMap<String, Object>();

		// 请求路径
		paramsParser.action = getString(R.string.me_feedback_url);
		// 请求方式
		paramsParser.request = "post";

		// 请求参数
		SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
		String userid = UserKeeper.getCurrentUser(this).getUserid();

		// 请求参数
		params.put("userid", userid);
		params.put("apptype", "1001");
		params.put("content", content);

		paramsParser.map = params;
		// 传递上下文
		paramsParser.context = this.context;

		AsyncHttpClient asyncHttpClient = AsyncHttpClient.getInstance();
		asyncHttpClient.exeute(paramsParser, new OnResultListenerAdapter() {

			@Override
			public <T extends Parser> void onSuccess(T resultParser,
					int resultCode) {
				super.onSuccess(resultParser, resultCode);
				Message message = Message.obtain();
				message.arg1 = resultCode;
				message.obj = resultParser.back;
				mHandler.sendMessage(message);
			}
			
			public <T extends Parser> void onFailure(T parserResult, int resultCode) {
				  runOnUiThread(new  Runnable() {
					public void run() {
						Toast.makeText(FeedbackActivity.this, "您的网络不好,反馈失败", Toast.LENGTH_SHORT).show();
					}
				});
			};
			
			public <T extends Parser> void onNoNetWork(T paramParser, int resultCode) {
				  runOnUiThread(new  Runnable() {
						public void run() {
							Toast.makeText(FeedbackActivity.this, "没有网络,请重试", Toast.LENGTH_SHORT).show();
						}
					});
			};
		});
		
	}
}
